package com.maskota;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class ListaCompras extends Activity {
	public UsuariosSQLiteHelper usdbh;
    public SQLiteDatabase db;
	String[] arrayid,arrayprod,arrayprecio,arrayurl;
	 private ListView list;
     private Resultados_Adapter adapter;
     Timer tim;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listaarticulos);
		
	    usdbh = new UsuariosSQLiteHelper(this, "Maskota", null, 1);

		
		obtenerArreglos();
		
        list = (ListView) findViewById(R.id.list);
        adapter=new Resultados_Adapter(ListaCompras.this, arrayprod, arrayprecio, arrayurl);
        list.setAdapter(adapter);
        list.setTextFilterEnabled(true);
        tim=new Timer();
        tim.schedule(new TimerTask() {
                @Override
                public void run() {
                        ListaCompras.this.runOnUiThread(new Runnable() {
                            public void run() {
                                        adapter.notifyDataSetChanged();
                            }
                        });
                }
        }, 0, 2000);
		
	
	}
	
	
    private void obtenerArreglos() {

	    db = usdbh.getWritableDatabase();
	    if(db != null)
	    {
	    
	        Cursor c = db.rawQuery("SELECT * FROM Articulos", null);
	        
	        arrayid=new String[c.getCount()];
	        arrayprod=new String[c.getCount()];
	        arrayprecio=new String[c.getCount()];
	        arrayurl=new String[c.getCount()];
	        int cont=0;
	    	if (c.moveToFirst()) {
	    	     do {
	    	    	  int id=c.getInt(0);
	    	          String articulo = c.getString(1);
	    	          Float precio = c.getFloat(2);
	    	          String urlfoto=c.getString(3);
	    	          
	    	          arrayid[cont]=""+id;
	    	          arrayprod[cont]=articulo;
	    	          arrayprecio[cont]=""+precio;
	    	          arrayurl[cont]=urlfoto;
	    	          

	    	          Log.i("BASE ARTICULOS", "id: "+id+" articulo: "+articulo+" precio: "+precio+" url: "+urlfoto);

	    	          cont++;
	    	          
	    	     } while(c.moveToNext());
	
	
	                       	  
	        }
	
	    	db.close();
	
	    }           
    }
}
